package com.tanhua.server.exceptions;

//统一异常处理的

import com.tanhua.server.vo.ErrorResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice  //处理在Controller层向上跑的异常
public class BasicExceptionAdvice {

//    处理自己的抛的异常
    @ExceptionHandler(TanhuaException.class)
    public ResponseEntity handlerException(TanhuaException e){
        return ResponseEntity.status(e.getStatusCode()).body(new ErrorResult(e));  //返回一个对象：有三个属性 状态码、错误信息、时间
    }

//    处理意料之外的异常
    @ExceptionHandler(Exception.class)
    public ResponseEntity handlerException(Exception e){
        e.printStackTrace();
        return ResponseEntity.status(500).body("服务器内部错误");
    }


}
